﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ENT.GP.Core.Estadisticas;
using ENT.GP.Core.Core;
using entgpguayas.Code;
using Ext.Net;
using ENT.GP.Core;
using System.Xml;
using System.Xml.Xsl;
using System.Web.UI.DataVisualization.Charting;
using ENT.GP.Core.Lookup;

namespace entgpguayas.Estadisticas.DosVariables
{
    public partial class Estadistica : PaginasUsuario
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
            //if (!IsPostBack)
            //{
                storeOpcionDatos.DataSource = OpcionVariables.GetOpcionVariables();
                storeOpcionDatos.DataBind();
                storeTipo.DataSource = TipoLookup.GetNameValueList(this.IdEmp);
                storeTipo.DataBind();

            //}
        }
        //protected void cbxOpcionDatos_Change(object sender, EventArgs e)
        protected void VerGrafica_Click(object sender, EventArgs e)
        {
            try
            {
                string Campo = cbxOpcionDatos.SelectedItem.Value;
                int tipoId;
                int.TryParse(cbxCategorias.SelectedItem.Value,out tipoId);
                var datos = DatosEstadisticaDosVariables.GetDatosEstadisticaDosVariables(Campo,270,0,this.IdEmp);
                var datosEstadistica = DatosEstadisticaDosVariables.GetDatosDeEstadistica(Campo, 270, 0, this.IdEmp);
                storeDatos.DataSource = datosEstadistica;
                storeDatos.DataBind();
                Chart1.Titles[0].Text = string.Format("Proyectos - {0}",cbxOpcionDatos.SelectedItem.Text);
                gdpDatos.Title= string.Format("Datos para: Proyectos - {0}", cbxOpcionDatos.SelectedItem.Text);
                                              

                this.Chart1.Series[1].ToolTip = "#AXISLABEL Número Proyectos: #VAL{0}";
                this.Chart1.Series[0].ToolTip = "#AXISLABEL Inversión: #VALY{C}";
                this.Chart1.Series[2].ToolTip = "#AXISLABEL Aporte Plan: #VALY{C}";
                this.Chart1.Series[3].ToolTip = "#AXISLABEL Aporte Contraparte 1: #VALY{C}";
                this.Chart1.Series[4].ToolTip = "#AXISLABEL Aporte Contraparte 2: #VALY{C}";
                foreach (var dato in datos)
                {
                    Chart1.Series[0].Points.AddXY(dato.Valor,dato.InversionTotal);
                   // Chart1.Series[0].IsValueShownAsLabel = true;
                    Chart1.Series[0].LabelFormat = "C";
                    Chart1.Series[1].Points.AddXY(dato.Valor, dato.NumeroProyectos);
                   // Chart1.Series[1].IsValueShownAsLabel = true;
                    Chart1.Series[2].Points.AddXY(dato.Valor, dato.AportePlan);
                   // Chart1.Series[2].IsValueShownAsLabel = true;
                    Chart1.Series[2].LabelFormat = "C";
                    Chart1.Series[3].Points.AddXY(dato.Valor, dato.AporteContraparte1);
                    //Chart1.Series[3].IsValueShownAsLabel = true;
                    Chart1.Series[3].LabelFormat = "C";
                    Chart1.Series[4].Points.AddXY(dato.Valor, dato.AporteContraparte2);
                    //Chart1.Series[4].IsValueShownAsLabel = true;
                    Chart1.Series[4].LabelFormat = "C";
                    
                }
                Chart1.Series[0].IsVisibleInLegend = true;
                Chart1.Series[1].IsVisibleInLegend = true;
                Chart1.Series[2].IsVisibleInLegend = true;
                Chart1.Series[3].IsVisibleInLegend = true;
                Chart1.Series[4].IsVisibleInLegend = true;
                //dato.Valor,dato.InversionTotalChart1.ChartAreas[0].AxisY.LabelStyle.Format = "C";
                Chart1.Series[0].LabelAngle = 30;
                Chart1.Series[0].Enabled = chkOpc1.Checked;
                Chart1.Series[1].Enabled = chkOpc2.Checked;
                Chart1.Series[2].Enabled = chkOpc3.Checked;
                Chart1.Series[3].Enabled = chkOpc4.Checked;
                Chart1.Series[4].Enabled = chkOpc5.Checked;
            }
            catch (Exception ex)
            {
                Util.RegistarLog(ex.ToString());
            }
        }

        protected void ToExcel(object sender, EventArgs e)
        {
            string json = GridData.Value.ToString();
            StoreSubmitDataEventArgs eSubmit = new StoreSubmitDataEventArgs(json, null);
            XmlNode xml = eSubmit.Xml;

            this.Response.Clear();
            this.Response.ContentType = "application/vnd.ms-excel";
            this.Response.AddHeader("Content-Disposition", "attachment; filename=reporteDatosEstadistica.xls");
            XslCompiledTransform xtExcel = new XslCompiledTransform();
            xtExcel.Load(Server.MapPath("Excel.xsl"));
            xtExcel.Transform(xml, null, this.Response.OutputStream);
            this.Response.End();
        }
    }
}
